package com.qys.livesMall.system.source.controller;


import com.qys.livesMall.common.result.R;
import com.qys.livesMall.common.utils.QysStringUtils;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.framework.view.QysPage;
import com.qys.livesMall.system.source.domain.dto.dict.AddDict;
import com.qys.livesMall.system.source.domain.dto.dict.DictDataSearch;
import com.qys.livesMall.system.source.domain.dto.dict.EditDict;
import com.qys.livesMall.system.source.domain.vo.dict.DictDateVO;
import com.qys.livesMall.system.source.domain.vo.dict.DictVO;
import com.qys.livesMall.system.source.service.ISysDictDataService;
import com.qys.livesMall.system.source.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author wanglei
 */
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController {
    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @GetMapping("/list")
    public R<QysPage<DictVO>> list(DictDataSearch search,@RequestHeader("languageType") String languageType) {
        search.setLanguageType(languageType);
        search.setLanguageType(SecurityUtils.getLanguage());
        return R.ok(dictDataService.selectDictDataList(search));
    }


    /**
     * 查询字典数据详细
     */
    @GetMapping(value = "/{dictCode}")
    public R<DictDateVO> getInfo(@PathVariable Long dictCode) {
        return dictDataService.selectDictDataById(dictCode);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")//@RequestHeader("languageType") String languageType
    public R<List<DictVO>> dictType(@PathVariable String dictType) {
        List<DictVO> data = dictTypeService.selectDictDataByType(dictType, SecurityUtils.getLanguage());
        if (QysStringUtils.isNull(data)) {
            data = new ArrayList();
        }
        return R.ok(data);
    }

    /**
     * 新增字典类型
     */
    @PostMapping
    public R add(@Validated @RequestBody AddDict dict) {
        return dictDataService.insertDictData(dict);
    }

    /**
     * 修改保存字典类型
     */
    @PutMapping
    public R edit(@Validated @RequestBody EditDict editDict) {
        return dictDataService.updateDictData(editDict);
    }

    /**
     * 删除字典类型
     */
    @DeleteMapping("/{dictCodes}")
    public R remove(@PathVariable Long[] dictCodes) {
        dictDataService.deleteDictDataByIds(dictCodes);
        return R.ok();
    }
}
